package com.demo.controller;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.model.UrmUser;
import com.demo.service.AccountService;
import com.demo.service.GeneralService;


/**
 * @ClassName: CommonController
 * @Description:
 */
@Controller
public class CommonController {

	@Resource
	private GeneralService service;
	
	@Resource
	private AccountService accountService;
	
	@RequestMapping(value="/login")
	public String login(Model model){
		System.out.println("=====================");
		model.addAttribute("date", new Date());
		UrmUser user = service.getUser(101853L);
		model.addAttribute("user", user);
		accountService.getAccount();
		//accountService.saveTwo();
		return "login";
	}
	
	@RequestMapping(value="/json")
	@ResponseBody
	public UrmUser getJson(Date date){
		UrmUser user = service.getUser(101853L);
		return user;
	}
	
	@RequestMapping(value="/getSecUser")
	public String getSecUser(Model model){
		UserDetails user = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		model.addAttribute("secUser", user);
		return "login";
	}
	
	@RequestMapping(value="/success")
	@PreAuthorize("hasPermission('ROLE_USER')")
	public String testsuc(){
		System.out.println("==============suc=======");
		return "login";
	}
	
	@RequestMapping(value="/deny")
	@PreAuthorize("hasPermission('deny')")
	public String deny(){
		System.out.println("===========deny==========");
		return "login";
	}
}
